<template>
  <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit3">
    <FormItem prop="userName">
      <Input v-model="form.userName" placeholder="请输入用户名">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="phoneNumber">
      <Input  v-model="form.phoneNumber" placeholder="请输入手机号">
        <span slot="prepend">
          <Icon :size="14" type="md-menu"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password" v-model="form.password" placeholder="请输入密码">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="password2" >
      <Input type="password" v-model="password2" placeholder="请再次输入确认密码" >
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="otp" style="display: inline-block;">
      <Input type="password" v-model="form.otp" placeholder="请输入验证码" style="width: 150px;">
        <span slot="prepend">
          <Icon :size="14" type="md-home"></Icon>
          
        </span>
      </Input>
    </FormItem>
    <div v-if="!sendMsgDisabled" @click="count" style="margin-bottom: 10px;" class="niceButton">获取验证码</div>
    <div v-if="sendMsgDisabled" class="niceButton" >{{ time+'秒后获取' }}</div>


    <FormItem>
      <Button @click="handleSubmit3" type="primary" long>注册</Button>
    </FormItem>
  </Form>
</template>

<script>
import {
  getOtp
} from '@/api/user'
export default {
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    phoneRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  data () {
    return {
      form: {
        userName: '',
        phoneNumber:'',
        password: '',
        otp:''
      },
      password2:'',
      time:60,
      sendMsgDisabled:false
    }
  },
  computed: {
    rules () {
      return {
        userName: this.userNameRules,
        password: this.passwordRules,
        phoneNumber: this.phoneRules

      }
    }
  },
  methods: {
    handleSubmit3 () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid3', {
            userName: this.form.userName,
            phoneNumber: this.form.phoneNumber,
            password: this.form.password,
            opt:this.form.otp
          })
        }
      })
    },
    count(){
      console.log(this.form.password)
      console.log(''+this.password2)
      if(this.form.password != this.password2){
        this.$message.error("两次输入的密码不一致！")
        return 
      }
      const that = this
      let phoneNumber = that.form.phoneNumber
      getOtp({phoneNumber })
        that.sendMsgDisabled = true
        const interval = window.setInterval(function() {
          if ((that.time--) <= 0) {
            that.time = 60
            that.sendMsgDisabled = false
            window.clearInterval(interval)
          }
        }, 1000)
    },

  }
}
</script>
<style>
 .niceButton {
              align-items: center;
              background: #FFFFFF;
              border: 0 solid #E2E8F0;
              box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
              box-sizing: border-box;
              color: #1A202C;
              display: inline-flex;
              font-family: Inter, sans-serif;
              font-size: 0.8rem;
              /* font-weight: 700; */
              height: 30px;
              justify-content: center;
              line-height: 16px;
              overflow-wrap: break-word;
              /* padding: 24px; */
              text-decoration: none;
              width: auto;
              /* border-radius: 8px; */
              cursor: pointer;
              user-select: none;
              -webkit-user-select: none;
              touch-action: manipulation;
            }

</style>